<script setup lang="ts">
import { RouterView } from 'vue-router'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMenu, ElMenuItem, ElMenuItemGroup, ElSubMenu, ElIcon } from 'element-plus'
import { User, Lock, Tickets, Money, DataLine, Setting } from '@element-plus/icons-vue'

const router = useRouter()
const activeIndex = ref('1')

// 检查是否已登录
onMounted(() => {
  const token = localStorage.getItem('admin_token')
  if (!token && router.currentRoute.value.path !== '/login') {
    router.push('/login')
  }
  
  // 根据当前路由设置激活菜单项
  const path = router.currentRoute.value.path
  if (path === '/') activeIndex.value = '1'
  else if (path === '/admins') activeIndex.value = '2-1'
  else if (path === '/roles') activeIndex.value = '2-2'
  else if (path === '/permissions') activeIndex.value = '2-3'
  else if (path === '/recharge-orders') activeIndex.value = '3-1'
  else if (path === '/balance-orders') activeIndex.value = '3-2'
  else if (path === '/financial-records') activeIndex.value = '3-3'
  else if (path === '/payment-configs') activeIndex.value = '4-1'
})

// 处理菜单选择
const handleSelect = (key: string) => {
  switch (key) {
    case '1':
      router.push('/')
      break
    case '2-1':
      router.push('/admins')
      break
    case '2-2':
      router.push('/roles')
      break
    case '2-3':
      router.push('/permissions')
      break
    case '3-1':
      router.push('/recharge-orders')
      break
    case '3-2':
      router.push('/balance-orders')
      break
    case '3-3':
      router.push('/financial-records')
      break
    case '4-1':
      router.push('/payment-configs')
      break
  }
}

// 处理退出登录
const handleLogout = () => {
  localStorage.removeItem('admin_token')
  router.push('/login')
}
</script>

<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <div class="header-content">
          <div class="logo">Admin Dashboard</div>
          <div class="user-info">
            <el-button @click="handleLogout">退出登录</el-button>
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
            :default-active="activeIndex"
            class="el-menu-vertical-demo"
            @select="handleSelect"
          >
            <el-menu-item index="1">
              <el-icon><DataLine /></el-icon>
              <span>首页</span>
            </el-menu-item>
            
            <el-sub-menu index="2">
              <template #title>
                <el-icon><User /></el-icon>
                <span>权限管理</span>
              </template>
              <el-menu-item index="2-1">管理员管理</el-menu-item>
              <el-menu-item index="2-2">角色管理</el-menu-item>
              <el-menu-item index="2-3">权限管理</el-menu-item>
            </el-sub-menu>
            
            <el-sub-menu index="3">
              <template #title>
                <el-icon><Money /></el-icon>
                <span>财务管理</span>
              </template>
              <el-menu-item index="3-1">充值订单</el-menu-item>
              <el-menu-item index="3-2">余额支付订单</el-menu-item>
              <el-menu-item index="3-3">财务记录</el-menu-item>
            </el-sub-menu>
            
            <el-sub-menu index="4">
              <template #title>
                <el-icon><Setting /></el-icon>
                <span>系统设置</span>
              </template>
              <el-menu-item index="4-1">支付配置</el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <el-main>
          <RouterView />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style scoped>
.common-layout {
  height: 100vh;
}

.el-header {
  background-color: #409eff;
  color: #fff;
  line-height: 60px;
  padding: 0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.logo {
  font-size: 20px;
  font-weight: bold;
}

.el-aside {
  background-color: #f5f5f5;
  border-right: 1px solid #e6e6e6;
}

.el-main {
  background-color: #fff;
  padding: 20px;
}

.el-menu {
  border-right: none;
}
</style>
